﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ECGadmin.Common;


namespace ECGadmin.DAL
{
    public partial class BannerDb
    {
        /// <summary>
        /// Lấy danh sách banner
        /// </summary>
        /// <param name="page"></param>
        /// <param name="itemsPerPage"></param>
        /// <param name="totalPage"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static List<ViewBannerDto> GetAllBanner(int page, int itemsPerPage, ref int totalPage, ref int totalRecord)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Banner_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));

                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                SqlParameter itemTotalRecord = command.Parameters.Add("@TotalRecord", SqlDbType.Int);
                itemTotalRecord.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ViewBannerDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                totalRecord = int.Parse(itemTotalRecord.Value.ToString() == "" ? "0" : itemTotalRecord.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về danh sách banner theo PageId và PositionId
        /// </summary>
        /// <param name="pageId"></param>
        /// <param name="positionId"></param>
        /// <returns></returns>
        public static List<BannerDto> GetBannerByPageIdPositionId(int pageId, int positionId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Banner_GetAllByPageIdPositionId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@PageId", pageId));
                command.Parameters.Add(dao.CreateParameter("@PositionId", positionId));
                return dao.GetDTOList<BannerDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
